package com.mvp.shq.washer.ui.shop.comment;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.OrderProductCommentAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.result.OrderDataListResult;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.entity.shop.CommentEntity;
import com.mvp.shq.washer.entity.shop.CommentResponse;
import com.mvp.shq.washer.entity.shop.ProductUnit;
import com.mvp.shq.washer.entity.shop.result.MyOrderListRestult;
import com.mvp.shq.washer.entity.shop.result.OrderDetailListRestult;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.SpUtil;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CommentActivity extends BaseActivity<Presenter, Model> implements CommentConstract.View {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.img_anonymous)
    ImageView imgAnonymous;
    @BindView(R.id.toolbar_menu)
    TextView toolbarMenu;
    private List<OrderDetailListRestult> datas;
    private OrderProductCommentAdapter adapter;
    private boolean isAnony = true;
    private List<CommentEntity> commentEntities;
    private OrderDataListResult orderInfo;

    @Override
    public int getLayoutId() {
        return R.layout.activity_comment;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        title.setText("商品评价");
        toolbarMenu.setText("发布");
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 10, ContextCompat.getColor(context, R.color.shop_divison_bg));
        recyclerView.addItemDecoration(divider);
        datas = new ArrayList<>();
        commentEntities = new ArrayList<>();
        adapter = new OrderProductCommentAdapter(context,datas);
        adapter.setListener(new OrderProductCommentAdapter.OnCommentContentChangeListener() {
            @Override
            public void onEdittextChange(int position, String content) {
                commentEntities.get(position).setContent(content);
            }

            @Override
            public void onRatingBarChange(int position, float rating) {
                commentEntities.get(position).setStar((double) rating);
            }
        });
        recyclerView.setAdapter(adapter);
        RxBus.get().send(EventCode.SHOP_SEND_ORDER_FRO_COMMENT);
    }
    @Subscribe(code = EventCode.SHOP_GET_ORDER_FRO_COMMENT)
    public void getOrderInfo(OrderDataListResult restult){
        datas.addAll(restult.getEcOrderDetailList());
        adapter.notifyDataSetChanged();
        User user = SpUtil.getUser();
        for (int i=0;i<datas.size();i++){
            OrderDetailListRestult orderInfo = datas.get(i);
            CommentEntity entity = new CommentEntity(orderInfo.getProductUnitId(),user.getId(),user.getFaceurlWithOutUrl(),user.getNickname(),
                    "1","匿名用户",restult.getCompanyId(),restult.getCompanyName(),restult.getId(),5,"");
            commentEntities.add(entity);
        }
    }


    @OnClick({R.id.back, R.id.toolbar_menu, R.id.rl_anonymous})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.toolbar_menu:
                CommentResponse response = new CommentResponse();
                response.setList(commentEntities);
                showLoadingDialog(true);
                presenter.comment(response);
                break;
            case R.id.rl_anonymous:
                isAnony = !isAnony;
                String str = "1";
                if (isAnony){
                    str = "1";
                    imgAnonymous.setImageResource(R.mipmap.cart_selected);
                }else {
                    str = "0";
                    imgAnonymous.setImageResource(R.mipmap.cart_pressed);
                }
                for (int i=0;i<commentEntities.size();i++){
                    commentEntities.get(i).setAnonym(str);
                }
                break;
        }
    }

    @Override
    public void commentS() {
        finish();
        RxBus.get().send(EventCode.SHOP_REFRESH_ORDER_LIST);
    }
}
